import axios from "axios";
import { ElMessage } from 'element-plus'
import type { AxiosInstance, InternalAxiosRequestConfig, AxiosError, AxiosResponse } from "axios";
const server: AxiosInstance = axios.create({
    baseURL: import.meta.env.VITE_API_URL,
    timeout: 5000
})
// 请求拦截器
server.interceptors.request.use((config: InternalAxiosRequestConfig) => {
    return config
}, (error: AxiosError) => {
    return Promise.resolve(error)
})

// 响应拦截器
server.interceptors.response.use((response: AxiosResponse) => {
    if (response.data.code !== 200) {
        ElMessage({
            message: response.data.message,
            type: 'warning',
            plain: true,
        })
    } else {
        return response.data
    }

}, (error: AxiosError) => {

    return Promise.resolve(error)
})

export default server